import java.io.PrintWriter;
import java.util.HashMap;

class VariableDeclaration extends Declaration {

	private Node type_;
	private Node type2_;
	private String name_;

	public VariableDeclaration(Node t, String n) {
		type_ = t;
		name_ = n;
	}

	public VariableDeclaration(Node t1, String n, Node t2) {
		type_ = t1;
		name_ = n;
		type2_ = t2;
	}

	@Override
	public void print(PrintWriter pw) {
		pw.println("Nonde name : VariableDeclaration. Var name is: " + name_);
		if (type2_ != null) {
			type2_.print(pw);
		}
		type_.print(pw);
	}

	public String translate(HashMap<String, PrintWriter> pws, String condition, SymbolTable rt,
			String parent) {
		String key = rt.getStack().get(1);
		PrintWriter pw = pws.get(key);
		// Level
		int level = rt.getLevel();
		
		String sc = "";
		
		for(int i = 1; i < rt.getStack().size(); i++) {
			if(i != rt.getStack().size() - 1) {
				sc += rt.getStack().get(i) + ";";
			} else {
				sc += rt.getStack().get(i);
			}
		}
		
		boolean isInitial;
		
		if(type2_ == null) {
			isInitial = false;
		} else {
			isInitial = true;
		}
		
		Symbol symbol = new Symbol(name_, type_, isInitial, sc, null);
		rt.getTables().add(symbol);
		/*String stemp = "";
		for(int i = 0; i < rt.getStack().size(); i++) {
			stemp += rt.getStack().get(i);
		}*/

//		Iterator<Symbol> iterator = rt.getTables().iterator();
//		while (iterator.hasNext()) {
//			Symbol sym = iterator.next();
//			System.out.println("-----------------------------");
//			System.out.println("Symbol Name: " + sym.getName());
//			System.out.println("Symbol Scope: " + stemp);
//		}
		System.out.println("VariableDeclaration: " + condition + " " + type2_
				);
		String strSimpleType = "";
		for(int i = 0; i < level * 4; i++) {
			pw.print(" ");
		}
		if (condition == null || !condition.equals("for")) {
			strSimpleType = type_.translate(pws, "private ", rt, parent);
		} else  {
			strSimpleType = type_.translate(pws, "", rt, parent);
		}
		pw.print(name_);
		if(strSimpleType.equals("ArrayList<String>")){
				pw.print(" = new ArrayList<String>();");
		}
		else {
			if (type2_ != null) {
				pw.print("=");
				type2_.translate(pws, null, rt, parent);
			}
			pw.println(";");

			if (condition == null || !condition.equals("for")) {
				String s1 = "";
				String s2 = "";	
				for(int i = 0; i < level * 4; i++) {
					s1 += " ";
				}
				s2 = s1 + "    ";
			
				pw.print(s1);
				pw.println("public void set" + name_ + "(" + strSimpleType
					+ " input) {");
				pw.print(s2);
				pw.println("this." + name_ + "=input;");
				pw.print(s1);
				pw.println("}");
				pw.print(s1);
				pw.println("public " +strSimpleType+" get" + name_ + "() {");
				pw.print(s2);
				pw.println("return this." + name_ + ";");
				pw.print(s1);
				pw.println("}");
			}
		}
		return null;
	}

}
